import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/components/Layout/index.vue'

// 导入页面组件
const Dashboard = () => import('@/views/dashboard/index.vue')
const Users = () => import('@/views/users/index.vue')
const Credits = () => import('@/views/credits/index.vue')
const Orders = () => import('@/views/orders/index.vue')
const VIP = () => import('@/views/vip/index.vue')
const AiImage = () => import('@/views/ai-image/index.vue')
const UserWorks = () => import('@/views/user-works/index.vue')
const System = () => import('@/views/system/index.vue')
const SystemLogs = () => import('@/views/system/logs.vue')
const Profile = () => import('@/views/profile/index.vue')
const Login = () => import('@/views/login/index.vue')

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      hideInMenu: true
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: {
          title: '仪表盘',
          icon: 'DataBoard'
        }
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
        meta: {
          title: '用户管理',
          icon: 'User'
        }
      },
      {
        path: '/credits',
        name: 'Credits',
        component: Credits,
        meta: {
          title: '积分管理',
          icon: 'GoldMedal'
        }
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders,
        meta: {
          title: '订单管理',
          icon: 'ShoppingBag'
        }
      },
      {
        path: '/vip',
        name: 'VIP',
        component: VIP,
        meta: {
          title: 'VIP管理',
          icon: 'Crown'
        }
      },
      {
        path: '/ai-image',
        name: 'AiImage',
        component: AiImage,
        meta: {
          title: 'AI生图统计',
          icon: 'Picture'
        }
      },
      {
        path: '/user-works',
        name: 'UserWorks',
        component: UserWorks,
        meta: {
          title: 'AI社区作品',
          icon: 'PictureRounded'
        }
      },
      {
        path: '/system',
        name: 'System',
        component: System,
        meta: {
          title: '系统管理',
          icon: 'Setting'
        }
      },
      {
        path: '/system/logs',
        name: 'SystemLogs',
        component: SystemLogs,
        meta: {
          title: '操作日志',
          icon: 'Document',
          hideInMenu: true
        }
      },
      {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: {
          title: '个人设置',
          icon: 'User',
          hideInMenu: true
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory('/admin/'),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token')
  
  if (to.path === '/login') {
    if (token) {
      next('/dashboard')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
